package com.fresh.controller;

import com.fresh.common.ResponseResult;
import com.github.pagehelper.PageInfo;
import com.fresh.entity.*;
import com.fresh.feignclient.ChengPinFeignClient;
import com.fresh.service.LingLiaoService;
import com.fresh.util.SnowflakeIdWorker;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/lingliao")
public class LingLiaoController {
    @Autowired
    SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    LingLiaoService lingLiaoService;
    @Autowired
    ChengPinFeignClient chengPinFeignClient;

    @GetMapping({"/list/{pageNum}"})
    public Object list(RawOutQO rawOutQO, @PathVariable(required = false) Integer pageNum) {
        if (pageNum == null) {
            pageNum = 1;
        }
        PageInfo<RawOut> pageInfo = lingLiaoService.findPageInfoByCondition(rawOutQO, pageNum, 4);
        return new ResponseResult(200, "success", pageInfo);

    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/add")
    public Object add(@RequestBody RawOut rawOut) {
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String data = sdf.format(dt);
        String data1 = sdf1.format(dt);

        rawOut.setRoId(snowflakeIdWorker.nextId());
        rawOut.setRoNo("LL" + data);
        rawOut.setRoIn(data1);
        lingLiaoService.add(rawOut);
        return new ResponseResult(200, "success", null);
    }

    @PostMapping("/update")
    public Object update(@RequestBody String roNo) {
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String data = sdf.format(dt);

        lingLiaoService.update(roNo, data);
        return new ResponseResult(200, "success", null);
    }

//    @Transactional(rollbackFor = Exception.class)
    @GlobalTransactional(name = "fsp-create-proIn", rollbackFor = Exception.class)
    @PostMapping("/proIn")
    public Object putIn(@RequestBody ProIn proIn) {
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String data = sdf.format(dt);

        lingLiaoService.changeStatus2ByRoNo(proIn.getRoNo(), data);
        return chengPinFeignClient.add(proIn);
    }

}
